var express = require('express');
var router = express.Router();
const sql = require("../sql/sql");//引入增删改查命令
const search = require("../sql/search.js");//引入查询的数据集合
const tools = require("../tools/index.js")
const uuid = require('node-uuid')
/* GET home page. */
//获取搜索历史的的数据
router.get('/', function(req, res, next) {
	const {userId} = req.query
	const token = req.headers.token
	tools.verify(token).then(()=>{
		sql.sort(search,{userId},{_id:0,__v:0},{time:-1}).then((data)=>{
			res.send({
				state:200,
				title:"请求成功",
				data
			})
		})
	}).catch(()=>{
		res.send({
			state:10086,
			title:"登录信息已失效，请重新登录"
		})
	})
})
/**
* @apiGroup 搜索历史的数据
* @api {get} /api/search 获取搜索历史数据
* @apiVersion 0.0.0
* @apiDescription 根据用户id来获取用户的搜索历史数据
* @apiSuccess {number} state 请求状态
* @apiParam {string} userId 用户的id
* @apiSuccess {string} title 请求说明
* @apiSuccess {array} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"请求成功",
*    "data":[{},{},...]
* }
* @apiError {number} state 请求状态
* @apiError {string} title 请求说明
* @apiError {string} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":201,
*    "title":"请求失败",
*    "data":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3001/api/search
*/

//添加历史搜索的数据
router.post('/add', function(req, res, next) {
	const {userId,searchText} = req.body
	const token = req.headers.token
	tools.verify(token).then(()=>{
		sql.find(search,{userId},{_id:0,__v:0}).then((data)=>{
			if(data.length<1){
				sql.insert(search,{
					searchId:uuid.v1(),
					userId,
					searchText,
					time:Date.now()
				}).then(()=>{
					res.send({
						state:200,
						title:'插入搜索关键字成功'
					})
				})
			}else{
				let arr = data
				let i=0
				let onoff=arr.some((val,index)=>{
					i = index
					return val.searchText === searchText
				})
				if(onoff){
					sql.update(search,{searchId:arr[i].searchId},{time:Date.now()}).then(()=>{
						res.send({
							state:200,
							title:'搜索关键字时间更新'
						})
					})
				}else{
					sql.insert(search,{
						searchId:uuid.v1(),
						userId,
						searchText,
						time:Date.now()
					}).then(()=>{
						res.send({
							state:200,
							title:'插入搜索关键字成功'
						})
					})
				}
	  	}
  	})
  }).catch(()=>{
		res.send({
			state:10086,
			title:'登录信息已失效，请重新登录'
		})
	})
})
  
/**
* @apiGroup 搜索历史的数据
* @api {post} /api/search/add 插入或修改搜索历史关键字数据
* @apiVersion 0.0.0
* @apiDescription 根据用户id和搜索关键字插入或更新关键字信息
* @apiParam {string} userId 用户的id
* @apiParam {string} searchTxet 搜索关键字
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"插入或更新时间成功",
* }
* @apiError {number} state 请求状态
* @apiError {string} title 请求说明
* @apiError {string} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":201,
*    "title":"请求失败",
*    "data":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3001/api/search/add
*/

router.post('/del', function(req, res, next) {
	const {userId,searchId} = req.body
	const token = req.headers.token
	tools.verify(token).then(()=>{
		sql.delete(search,{userId,searchId}).then(()=>{
			console.log(111111111)
			res.send({
				state:200,
				title:"删除成功"
			})
		})
	}).catch(()=>{
		res.send({
			state:10086,
			title:'登录信息已失效，请重新登录'
		})
	})
})
/**
* @apiGroup 搜索历史的数据
* @api {post} /api/search/del 删除索历史关键字数据
* @apiVersion 0.0.0
* @apiDescription 根据用户id和搜索id删除关键字信息
* @apiParam {string} userId 用户的id
* @apiParam {string} searchId 搜索id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"删除成功",
* }
* @apiError {number} state 请求状态
* @apiError {string} title 请求说明
* @apiError {string} data 请求数据
* @apiErrorExample {json} Error-Response:
* {
*    "code":201,
*    "title":"请求失败",
*    "data":"NOT DATA"
* }
* @apiSampleRequest http://localhost:3001/api/search/del
*/



module.exports = router;
